/*
 * Copyright 2012 JooKar BV (info@jookar.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.jookar.android.simpleclock.alarm;

import java.util.Date;

import nl.jookar.android.simpleclock.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 * @author Joost A. Bloemsma
 *
 */
public class AlarmDisplayActivity extends Activity {
	private static final String WL_TAG = "Wakelock_Tag";
	private static final String DEBUG_TAG =  AlarmDisplayActivity.class.getName();
	
	private TextView tvTime;
	private TextView tvMsg;
	private PowerManager pm; 
	private WakeLock wl = null;
	
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Make sure there is nothing else on the screen but the this 
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
		// Make sure this window is shown over the lockscreen
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
		
		setContentView(R.layout.alarmdisplay);
		getControls();
		Intent intent = getIntent();
		String time = (String)DateFormat.format(getString(R.string.fmt_24h), new Date());
		tvTime.setText(time);
		String msg = intent.getStringExtra(AlarmReceiver.PARM_MSG);
		tvMsg.setText(msg);
	}
	
	private void getControls() {
		tvTime = (TextView)findViewById(R.id.txtTime);
		tvMsg = (TextView)findViewById(R.id.txtMsg);
	}
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onStop() {
		super.onStop();
		if (wl != null) {
			wl.release();
			Log.d(DEBUG_TAG,"Released WakeLock");
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onResume() {
		super.onResume();
		pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		
		if (!pm.isScreenOn()) {
			Log.d(DEBUG_TAG,"Screen is off, acquiring WakeLock");
			wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, WL_TAG);
			wl.acquire();
		}
		
	}
}
